package example.controller;

import example.entity.Adminuser;
import example.service.impl.AdminuserServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.support.SessionStatus;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class loginctrl {

    //@Autowired
    @Resource
    AdminuserServiceImpl adminuserService;

    @GetMapping("/toLoginPage")
    public String gotologin() {
        return "login";
    }

    //@RequestMapping(value = "dologin")
    @PostMapping("/doLogin")
    public String doLogin(Adminuser adminuser, Model model, HttpSession session) {
        Adminuser admin = adminuserService.geAdminUser(adminuser);
        System.out.println(admin);
        if (admin == null) {
            model.addAttribute("text1", "用户名或者密码不正确");
            return "login";
        } else {
            session.setAttribute("uname",admin.getAduname());
            return "redirect:/admin/goods";
        }
    }
    @GetMapping("/logout")
    public String dologout(HttpSession session, SessionStatus sessionStatus){
        //只是让session失效 但session仍然存在
        session.invalidate();
        sessionStatus.setComplete();
        return "login";
    }
}
















